//
//  HMComposeToolBar.swift
//  Weibo
//
//  Created by teacher on 2017/6/13.
//  Copyright © 2017年 teacher. All rights reserved.


import UIKit

enum HMComposeToolBarButtonType: Int {
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  #
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
}

//  自定义撰写toolbar视图
//  UIStackView只是一个容器，不具备渲染效果，颜色有子控件决定
class HMComposeToolBar: UIStackView {

    //  执行点击toolbar按钮闭包
//    var callBack: ((Int)->())?
    var callBack: ((HMComposeToolBarButtonType)->())?
    //  表情按钮
    var emoticonButton: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    //  设置视图
    private func setupUI() {
        
        //  设置水平方向布局
        self.axis = .horizontal
        //  设置子视图填充方式
        self.distribution = .fillEqually
        // _ = 忽略结果
        addChildButton(imageName: "compose_toolbar_picture", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //  添加子按钮
    @discardableResult  // 忽略返回值
    private func addChildButton(imageName: String, type: HMComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        //  设置枚举的原始值(0,1,2,3,4)作为tag使用
        button.tag = type.rawValue
        //  添加点击事件
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        //  设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        //  去掉点击高亮
        button.adjustsImageWhenHighlighted = false
        //  注意点，添加子视图
        addArrangedSubview(button)
        return button
    }

    //  MARK:   --toolbar按钮点击事件处理
    @objc private func btnAction(btn: UIButton) {
        //  执行传入过来的闭包，完成数据的回调
//        callBack?(btn.tag)
        //  根据枚举的原始值获取枚举类型
        let type = HMComposeToolBarButtonType(rawValue: btn.tag)!
        callBack?(type)
    }
    
    //  根据键盘类型切换对应icon
    func switchButtonIcon(isEmoticonKeyBoard: Bool) {
        if isEmoticonKeyBoard {
            //  表情键盘对应的系统键盘的icon
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
            
        } else {
            //  系统键盘对应的表情键盘的icon
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
}
